//==============================================================================
//
// File: ShowSettings.cpp
// Project: Browser
// Author(s): Matthias Van Eeghem (matthias@van-eeghem.com)
// License: See LICENSE in root directory
//
// The ShowSettings dialog allows a user to change his/her name.
//
//==============================================================================

#include "SettingsDialog.h"
#include "ui_SettingsDialog.h"
#include "Constants.h"
#include <QSettings>
#include <QMessageBox>
#include <QString>
#include <QFileDialog>

// Constructor of the ShowSettings class
ShowSettings::ShowSettings(QWidget * parent) : QDialog(parent), ui(new Ui::ShowSettings)
{
    ui->setupUi(this);

    // Disable the icon and close button, looks nicer
    setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);

    // Read the playername and install dir from the settings and set the inputline
    QSettings settings;
    ui->nameEdit->setText(settings.value("PlayerName").value<QString>());
    ui->installDirEdit->setText(settings.value("InstallDir").value<QString>());
}
// Called when the "Browse" button is clicked (for the user to browse to the GTA SA install directory)
void ShowSettings::on_browseButton_clicked()
{
    QString SelectedDir = QFileDialog::getExistingDirectory(this, "Select your GTA: SA installation directory.");

    ui->installDirEdit->setText(SelectedDir);
}

// Called when the "OK" button is clicked
void ShowSettings::on_okButton_clicked()
{
    if(ui->nameEdit->text().length() > MAX_PLAYER_NAME)
    {
        QMessageBox::information(this, "Invalid name", "Your name cannot be longer than " + QString::number(MAX_PLAYER_NAME) + " characters.");
        return;
    }

    // Save the settings to the registry
    QSettings settings;
    settings.setValue("PlayerName", ui->nameEdit->text());
    settings.setValue("InstallDir", ui->installDirEdit->text());

    close();
}

// Called when the "Cancel" button is clicked
void ShowSettings::on_cancelButton_clicked()
{
    close();
}

ShowSettings::~ShowSettings()
{
    delete ui;
}
